package com.hc.common.bean;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.houbb.data.factory.core.util.DataUtil;
import com.hc.common.utils.JsonUtil;
import com.hc.demo.domain.MyBatisDept;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

/**
 * 封装服务器端返回的结果
 *
 * @author hecai
 */
@Getter
@ToString
public class Result {
    /**
     * 请求响应状态码
     */
    private int code;
    /**
     * 请求结果描述信息
     */
    private String msg;
    /**
     * 请求结果数据
     */
    private Object data;

    protected Result(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result setCode(int code) {
        this.code = code;
        return this;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    /**
     * 将key-value形式的成对出现的参数转换为JSON
     *
     * @param objs
     * @return
     */
    public Result setData(Object... objs) {
        if (objs.length % 2 != 0) {
            throw new RuntimeException("参数个数不对");
        }
        for (int i = 0; i < objs.length; i += 2) {
            if (!(objs[i] instanceof String)) {
                throw new RuntimeException("奇数参数必须为字符串");
            }
        }
        ObjectMapper objectMapper = new ObjectMapper();
        //下面两行解决Java8新日期API序列化问题
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        ObjectNode objectNode = objectMapper.createObjectNode();
        for (int i = 0; i < objs.length; i += 2) {
            objectNode.putPOJO((String) objs[i], objs[i + 1]);
        }
        this.data = objectNode;
        return this;
    }

    public static void main(String[] args) {
        MyBatisDept dept = DataUtil.build(MyBatisDept.class);

        MyBatisDept dept1 = DataUtil.build(MyBatisDept.class);
        MyBatisDept dept2 = DataUtil.build(MyBatisDept.class);
        MyBatisDept dept3 = DataUtil.build(MyBatisDept.class);
        List<MyBatisDept> deptList = List.of(dept1, dept2, dept3);

        Result result = ResultUtil.success().setData("dept", dept, "deptList", deptList);
        System.out.println(JsonUtil.obj2String(result));

    }
}
